import json
import random
import time
from tqdm import tqdm
from extractor import call_gpt


def main():
    with open("data/person.jsonl", "r", encoding="utf-8") as f:
        data = [i.strip() for i in f.readlines() if i.strip()]

    with open("data/result.jsonl", "w", encoding="utf-8") as f:
        for line in tqdm(data):
            line = json.loads(line)
            query = line["title"] + "\n\n\n" + line["summary"] + "\n\n\n" + line["basic-info"] + "\n\n\n"
            url = line["url"]
            try:
                res = call_gpt(query)
                f.write(json.dumps(json.loads(res), ensure_ascii=False) + "\t" + url + "\n")
            except KeyboardInterrupt:
                break
            except:
                print("error")
                time.sleep(120)
                continue
            
            time.sleep(random.random() * 3)
            


if __name__ == "__main__":
    main()
